package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.service.WechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WechatServiceImpl implements WechatService {
    // 登录
    private static final String REQUEST_URL = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code";

    // 获取token
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    // 获取手机号
    private static final String PHONE_REQUEST_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    @Value("${wechat.appId}")
    private String appId;

    @Value("${wechat.appSecret}")
    private String appSecret;

    /*
     * 获取微信用户openId
     * @param code 微信授权码
     * @return openId
     */
    @Override
    public String getOpenId(String code) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", appId);
        paramMap.put("secret", appSecret);
        paramMap.put("js_code", code);
        String result = HttpUtil.get(REQUEST_URL, paramMap);
        // 判断接口响应是否出错
        //将结果解析为json对象
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (ObjectUtil.isNotEmpty(jsonObject.getInstant("errcode"))) {
            throw new BaseException(jsonObject.getString("errmsg"));
        }

        //解析结果中的openId并返回
        return jsonObject.getString("openid");
    }

    /*
     * 获取微信用户手机号
     * @param detailCode 小程序端接口临时登录凭证
     * @return 手机号
     */
    @Override
    public String getPhone(String detailCode) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("code", detailCode);
        String result = HttpUtil.post(PHONE_REQUEST_URL + getAccessToken(), JSONUtil.toJsonStr(paramMap));
        JSONObject jsonObject = JSONObject.parseObject(result);
        // 判断接口响应是否出错
        if (!ObjectUtil.equals(jsonObject.getInteger("errcode"), 0)) {
            throw new BaseException(jsonObject.getString("errmsg"));
        }
        // 解析结果
        return jsonObject.getJSONObject("phone_info").getString("phoneNumber");
    }

    /*
     * 获取微信接口调用凭证
     * @return access_token
     */
    private String getAccessToken() {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", appId);
        paramMap.put("secret", appSecret);
        String result = HttpUtil.get(TOKEN_URL, paramMap);
        // 判断接口响应是否出错
        //将结果解析为json对象
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (ObjectUtil.isNotEmpty(jsonObject.getInstant("errcode"))) {
            throw new BaseException(jsonObject.getString("errmsg"));
        }
        return jsonObject.getString("access_token");
    }
}
